package com.quikin.persistence.jpa.controller.aa;

import java.util.Collection;

import com.quikin.persistence.jpa.entity.aa.AuthenticationInfo;
import com.von.platform.orm.QueryCommand;
import com.von.platform.orm.TransactionException;

public class AuthenticationInfoController extends AbstractController {

    private static AuthenticationInfoController instance;
    
    private AuthenticationInfoController() {
        
    }
    
    public static AuthenticationInfoController getInstance() {
        if (instance == null) {
            instance = new AuthenticationInfoController();
        }
        return instance;
    }
    
    public AuthenticationInfo getAuthenticationInfo(String profileId) throws TransactionException {
        QueryCommand cmd = new QueryCommand();
        cmd.setResultClass(AuthenticationInfo.class);
        cmd.setKey(profileId);
        return (AuthenticationInfo) this.find(cmd); 
    }
    
    public AuthenticationInfo getAuthenticationInfoByLoginId(String loginId) throws TransactionException {
        QueryCommand cmd = new QueryCommand();
        cmd.setResultClass(AuthenticationInfo.class);
        cmd.setNativeSQL("select a from AuthenticationInfo a where a.loginId=:loginId");
        cmd.addParam("loginId",loginId);
        @SuppressWarnings("unchecked")
        Collection<AuthenticationInfo> infos = (Collection<AuthenticationInfo>) findCustom(cmd);
        if (infos == null) {
            return null;
        }
        if (infos.isEmpty()) {
            return null;
        }
        if (infos.size() > 1) {
            return null;
        }
        return infos.iterator().next();
    }
}
